package com.ydlclass.controller;

import com.github.pagehelper.PageInfo;
import com.ydlclass.annotation.Log;
import com.ydlclass.annotation.Repeat;
import com.ydlclass.entity.QueryForm;
import com.ydlclass.entity.YdlUser;
import com.ydlclass.service.YdlUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 用户信息表(YdlUser)表控制层
 *
 * @author makejava
 * @since 2022-09-17 16:59:07
 */
@RestController
@Slf4j
@RequestMapping("ydlUser")
public class YdlUserController {
    /**
     * 服务对象
     */
    @Resource
    private YdlUserService ydlUserService;

    /**
     * 分页查询
     *
     * @param ydlUser     筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<YdlUser>> queryByPage(YdlUser ydlUser, PageRequest pageRequest) {
        return ResponseEntity.ok(this.ydlUserService.queryByPage(ydlUser, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<YdlUser> queryById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(this.ydlUserService.queryById(id));
    }

   //模糊查询所有并且分页
    @GetMapping("findUserByPage")
    @Log(content = "角色管理",businessType = "查询所有")
    public ResponseEntity<PageInfo<YdlUser>> queryAll(QueryForm queryForm) {
        YdlUser ydlUser=new YdlUser();
        ydlUser.setUserName(queryForm.getUserName());
        ydlUser.setNickName(queryForm.getNickName());

        System.out.println("进入findUserByPage");
        return ResponseEntity.ok(this.ydlUserService.findUserByPage(ydlUser,queryForm.getPageNum(), queryForm.getPageSize()));
    }
    //模糊查询
    @GetMapping("queryLike")
    @Log(content = "角色管理",businessType = "模糊查询")
    public ResponseEntity<List<YdlUser>> queryLike( YdlUser queryForm) {
        return ResponseEntity.ok(this.ydlUserService.queryLike( queryForm.getUserName(),queryForm.getNickName()));
    }
    /**
     * 新增数据
     *
     * @param ydlUser 实体
     * @return 新增结果
     */
    @Repeat(5)
    @PostMapping("/add")
    @Log(content = "角色管理",businessType = "新增")
    public ResponseEntity<YdlUser> add(@RequestBody YdlUser ydlUser) {
        ydlUser.setCreateTime(new Date());
        return ResponseEntity.ok(this.ydlUserService.insert(ydlUser));
    }

    /**
     * 编辑数据
     *
     * @param ydlUser 实体
     * @return 编辑结果
     */
    @PutMapping("edit")
    @Log(content = "角色管理",businessType = "编辑")
    public ResponseEntity<YdlUser> edit(@RequestBody YdlUser ydlUser) {
        ydlUser.setUpdateTime(new Date());
        return ResponseEntity.ok(this.ydlUserService.update(ydlUser));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("delete/{id}")
    @Log(content = "角色管理",businessType = "删除")
    public ResponseEntity<Boolean> deleteById(@PathVariable Long id) {
        return ResponseEntity.ok(this.ydlUserService.deleteById(id));
    }
    //获取权限信息
    @GetMapping("getInfo")
    public HashMap<String, Object> getInfo() {
        System.out.println("进入getInfo");
        HashMap<String, Object> info = this.ydlUserService.getInfo();
        return info;
    }

}

